<?php defined('SYSPATH') or die('No direct script access.');

	class Model_Services_ClinicalInformation extends Model implements Jqgrid_GridListener {
		
		public $clinicalInfo = NULL;
		protected $id_patient;
		
		public function __construct($id=false){ //este id es el de la informacion clinica
			if ($id) $this->clinicalInfo = $this->get($id);
		}	
		
		public function setPatient($id){
			$this->id_patient = $id;
		}
		
		public function count($criteria = false){
			return ORM::factory("dao_clinicalinformation")->where("patient_id","=",$this->id_patient)->count_all();
		}
		
		public function getList($criteria = false){
				
			if (!$criteria)	$clinical_information_list = ORM::factory("dao_clinicalinformation")->where("patient_id","=",$this->id_patient)->find_all()->as_array();
			else $clinical_information_list = ORM::factory("dao_clinicalinformation")->where("patient_id","=",$this->id_patient)->order_by($criteria->sidx,$criteria->sord)->limit($criteria->limit)->offset($criteria->calcOffset())->find_all()->as_array();
		
			$dataset = array();
			foreach ($clinical_information_list as $clinical_information){
				$dataset[$clinical_information->id] = array(
												Helper_Date::mysqlToDate($clinical_information->information_date),
												$clinical_information->age,
												$clinical_information->height,
												$clinical_information->weight,
												$clinical_information->observations,
												$clinical_information->total_cholesterol,
												$clinical_information->hdl,
												$clinical_information->ldl,
												$clinical_information->triglycerides,
												$clinical_information->glucemic,
												$clinical_information->systolic_pressure,
												$clinical_information->diastolic_pressure,
												$clinical_information->patient_id);
			}
					
			return $dataset;
		}
		
		
		/**
		*
		* get clinical information from an id
		* @param int $minId
		* @param int $maxAmount
		* @return array of clinical information
		*/
		public function getListFromMinID($minId, $maxAmount){
			return ORM::factory("dao_clinicalinformation")->where("id",">",$minId)->order_by("id", "ASC")->limit($maxAmount)->find_all()->as_array();
		}
		
		
		public function getCondition($orderfield, $ordertype, $start,$limit,$id_patient){
			return ORM::factory("dao_clinicalinformation")->where("patient_id","=",$this->id_patient)->order_by($orderfield,$ordertype)->limit($limit)->find_all()->as_array();
		}
		
		public function get($id){
			return ORM::factory("dao_clinicalinformation")->find($id);
		}
		
		public function calculateIMC(){
			//IMC = (Peso / (Altura X Altura))
			$denominator = pow(intval($this->clinicalInfo->height)*0.01,2);
			return ($denominator)? round(floatval($this->clinicalInfo->weight) / $denominator, 3): 0;	
		}
		
		public function getDiastolicPressure(){
			return $this->clinicalInfo->diastolic_pressure;
		}
		
		public function getHeight(){
			return $this->clinicalInfo->height;
		}
		
		public function getSystolicPressure(){
			return $this->clinicalInfo->systolic_pressure;
		}
		public function getWeight(){
			return $this->clinicalInfo->weight;
		}
		
		public function information_date(){
			return $this->clinicalInfo->information_date;
		}
		
		public function lipidicProfile(){
			$profile = array('hdl'=>'','ldl'=>'','triglycerides'=>'','total_cholesterol'=>'');
			$profile['hdl'] = $this->clinicalInfo->hdl;
			$profile['ldl'] = $this->clinicalInfo->ldl;
			$profile['triglycerides'] = $this->clinicalInfo->triglycerides;
			$profile['total_cholesterol'] = $this->clinicalInfo->total_cholesterol;
			return $profile;
		}
		
		public function glucemicValue(){
			return $this->clinicalInfo->glucemic;
		}
		
		public function observations(){
			return $this->clinicalInfo->observations;
		}
			
		public function save($id, array $data){
			
			$this->clinicalInfo = ($id) ? ORM::factory("dao_clinicalinformation",$id) : ORM::factory("dao_clinicalinformation");
			
			//hacer funcion para pasar de string a numeros (una funcion sanitize input)
			$this->clinicalInfo->information_date = Helper_Date::dateToMysql($data['information_date']);
			$this->clinicalInfo->age = intval($data['age']);
			$this->clinicalInfo->height = intval($data['height']);
			$this->clinicalInfo->weight = floatval($data['weight']);
			$this->clinicalInfo->observations = $data['observations'];
			$this->clinicalInfo->total_cholesterol = floatval($data['total_cholesterol']);
			$this->clinicalInfo->hdl = floatval($data['hdl']);
			$this->clinicalInfo->ldl = floatval($data['ldl']);
			$this->clinicalInfo->triglycerides = floatval($data['triglycerides']);
			$this->clinicalInfo->glucemic = floatval($data['glucemic']);
			$this->clinicalInfo->systolic_pressure = floatval($data['systolic_pressure']);
			$this->clinicalInfo->diastolic_pressure = floatval($data['diastolic_pressure']);
			$this->clinicalInfo->patient_id = ($id) ? intval($data['patient_id']) : $this->id_patient;
					
			$this->clinicalInfo->save();
			return $this->clinicalInfo->id;
		}
		
		public function delete($id){
			$this->clinicalInfo = ORM::factory("dao_clinicalinformation",$id);
			$this->clinicalInfo->delete();
			return $id;
		}
		
		public function export($filenamepath){
			
			$contenido  = "Fecha de incidencia clínica = ".Helper_Date::mysqlToDate($this->clinicalInfo->information_date)."\r\n";
			$contenido .= "Edad = ".$this->clinicalInfo->age."\r\n";
			$contenido .= "Peso (kg) = ".$this->clinicalInfo->weight."\r\n";
			$contenido .= "Altura (cms) = ".$this->clinicalInfo->height."\r\n";
			$contenido .= "Observaciones = ".$this->clinicalInfo->observations."\r\n";
			$contenido .= "-----------------------------------------------------------\r\n";
			$contenido .= "Colesterol Total = ".$this->clinicalInfo->total_cholesterol."\r\n";
			$contenido .= "Colesterol HDL = ".$this->clinicalInfo->hdl."\r\n";
			$contenido .= "Colesterol LDL = ".$this->clinicalInfo->ldl."\r\n";
			$contenido .= "Nivel de Trigliceridos = ".$this->clinicalInfo->triglycerides."\r\n";
			$contenido .= "Nivel Glucémico = ".$this->clinicalInfo->glucemic."\r\n";
			$contenido .= "Presión Sistólica = ".$this->clinicalInfo->systolic_pressure."\r\n";
			$contenido .= "Presión Diastólica = ".$this->clinicalInfo->diastolic_pressure."\r\n";
			
			$f=fopen($filenamepath,"w");
			fwrite($f,$contenido);
			fclose($f);
			return;
		}
	
		
	}
?>

